package demo.bookDemo;

import java.io.*;
import java.util.*;

public class BookTest {

    public static void main(String[] args) {

        ArrayList<Book> books = getBooks();
        //存放到ArrayList集合中然后分别通过三种方式遍历此集合中的Book对象，进行输出打印
        fun1(books);
        //对集合中的元素进行排序。排序规则：首先按照销量排序(降序)，如果销量高相等按照价格排序(降序) 可查阅Collections类 API文档
        fun2(books);
        //将集合中图书价格大于60的Book对象，放入到另一个集合并使用对象流保存到book1.txt文件中
        fun3(books);
        //将book1.txt文件中的内容读取出来，获取图书对象中的书名和作者，“三国演义-罗贯中”以这种字符串格式使用文件流保存到book2.txt文件中
        fun4();
        //6、book2.txt中的内容读取出来，将其保存到Map集合中，使用书名作为Key,作者作为Value。
        //如：put(“三国演义”,”罗贯中”);
        fun5();

    }

    private static void fun5() {
        Map<String,String> map = new HashMap<>();
        try (BufferedReader br = new BufferedReader(new FileReader("demo19\\src\\demo\\bookDemo\\book2.txt"));){
            String line;
            while((line = br.readLine()) != null) {
                String[] strings = line.split("-");
                map.put(strings[1],strings[0]);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(map);
    }

    private static void fun4() {

        //读对象
        try (
                ObjectInputStream oos = new ObjectInputStream(new FileInputStream("demo19\\src\\demo\\bookDemo\\book1.txt"));
                BufferedWriter bw = new BufferedWriter(new FileWriter("demo19\\src\\demo\\bookDemo\\book2.txt"));

        ) {
            ArrayList<Book> books2 = (ArrayList<Book>) oos.readObject();
            for (Book b : books2) {
                String author = b.getAuthor();
                String bookName = b.getBookName();
                bw.write(author+"-"+bookName);
                bw.newLine();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static void fun3(ArrayList<Book> books) {
        ArrayList<Book> books1 = new ArrayList<>();
        for (Book b : books) {
            if (b.getPrice() > 60) {
                books1.add(b);
            }
        }

        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("demo19\\src\\demo\\bookDemo\\book1.txt"));) {
            oos.writeObject(books1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }



    }

    private static void fun1(ArrayList<Book> books) {
        for (Book b : books) {
            System.out.println(b);
        }
        System.out.println();
        for (int i = 0; i < books.size(); i++) {
            System.out.println(books.get(i));
        }
        System.out.println();
        Iterator it = books.iterator();
        while(it.hasNext()) {
            System.out.println(it.next());
        }
    }

    private static void fun2(ArrayList<Book> books) {

        books.sort(new Comparator<Book>() {

            //例：o1 =4 ，o2 = 6；
            //	return o2 – o1； {即返回值为正数，即为1，那么前者权重大，排序为o2，o1；
            //即为6，4，值降序排列}
            //	return o1 – o2； {即返回值为负数，即为-1，那么后者权重大，排序为o1，o2；
            @Override
            public int compare(Book o1, Book o2) {
                if (Objects.equals(o1.getSaleNum(), o2.getSaleNum())){
                    return o2.getPrice() - o1.getPrice() > 0 ? 1 : -1;
                }
                return  o2.getSaleNum() - o1.getSaleNum();
            }
        });

        books.forEach(b -> System.out.println(b));
    }

    private static ArrayList<Book> getBooks() {
        ArrayList<Book> books = new ArrayList<>();

        Book book = new Book("1001", "Java从入门到精通", "张三", 99.9, 100);
        Book book2 = new Book("1002", "Python从入门到精通", "李四", 89.9, 100);
        Book book3 = new Book("1003", "C++从入门到精通", "王五", 79.9, 100);
        Book book4 = new Book("1004", "Go从入门到精通", "赵六", 69.9, 100);
        Book book5 = new Book("1005", "Rust从入门到精通", "燕七", 59.9, 100);

        books.add(book);
        books.add(book2);
        books.add(book3);
        books.add(book4);
        books.add(book5);
        return books;
    }

}
/**
*
* 按照实体类创建规范创建图书类Book，
* 包含五个属性（编号bookNum、书名bookName、作者author、价格price、销量saleNum）
* 无参构造器、有参构造器、seter、geter方法、重写toString方法
*
 * * */
class Book implements Serializable{
    private static final long serialVersionUID = 1L;

    private String bookNum;
    private String bookName;
    private String author;
    private Double price;
    private Integer saleNum;

    public Book() {
    }

    public Book(String bookNum, String bookName, String author, Double price, Integer saleNum) {
        this.bookNum = bookNum;
        this.bookName = bookName;
        this.author = author;
        this.price = price;
        this.saleNum = saleNum;
    }

    public String getBookNum() {
        return bookNum;
    }

    public void setBookNum(String bookNum) {
        this.bookNum = bookNum;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Integer getSaleNum() {
        return saleNum;
    }

    public void setSaleNum(Integer saleNum) {
        this.saleNum = saleNum;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookNum='" + bookNum + '\'' +
                ", bookName='" + bookName + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", saleNum=" + saleNum +
                '}';
    }

}
